package com.byxy.managerial.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.byxy.managerial.dao.DepartmentDao;
import com.byxy.managerial.entity.Department;
import com.byxy.managerial.entity.Staff;
import com.byxy.managerial.service.DepartmentService;

/**
 * 部门service接口实现类
 * 
 *
 */
@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

	@Resource
	private DepartmentDao departmentDao;

	@Override
	public boolean save(Department department) {
		if (departmentDao.getByName(department.getDname()) == null) {
			departmentDao.save(department);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean delete(Integer id) {
		Department department = departmentDao.getByID(id);
		Set<Staff> staffs = department.getStaff();// ��ȡ�������Ա��
		for (Staff s : staffs) {
			System.out.println(s);
			s.setDepart(null);// ���Ա���󶨵Ĳ���
		}
		departmentDao.delete(department);
		return true;
	}

	@Override
	public boolean update(Department department) {
		if (departmentDao.getByName(department.getDname()) != null) {
			if (departmentDao.getByName(department.getDname()).getId() == department.getId()) {
				departmentDao.update(department);//如果要修改的部门所属名称的id与数据库所在id相符可以修改
				return true;
			}else {
				//否则重复编号，不修改
				return false;
			}
		}else {
			//编号不存在，可以修改
			departmentDao.update(department);
			return true;
		}
	}

	@Override
	public List<Department> find() {
		return departmentDao.find();
	}

	@Override
	public int countTotal() {
		return (int) departmentDao.countAll();
	}

	@Override
	public List<Department> find(int pos, int row) {
		return departmentDao.find(pos, row);
	}

	@Override
	public Department getById(int id) {
		return departmentDao.getByID(id);
	}

	@Override
	public List<Department> findDim(String likeVal) {
		return departmentDao.findDim(likeVal);
	}

}
